package com.bjsj.pro.base;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.bjsj.pro.base.view.NetworkLayout;
import com.bjsj.pro.base.view.TitleBarLayout;
import com.bjsj.pro.tools.GlideManager;
import com.bjsj.pro.tools.GlobalTools;
import com.bjsj.pro.tools.SoftKeyBoardListener;
import com.lzy.okgo.OkGo;
import com.bjsj.pro.R;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by Nevermore on 2017/8/14.
 */

public abstract class BaseUiFragment<V, T extends MVPBasePresenter<V>> extends LazyFragment
{
    protected NetworkLayout failnetworkd;;
    protected FrameLayout fl_baseFragment_bottom;//帧布局控件
    protected View view;//View控件
    protected T mPresenter;
    protected TitleBarLayout mTitleBarLayout;
    protected View stater;
    protected GlobalTools tools;
    private Unbinder mUnbinder;
    private View mBaseView;
    protected FragmentActivity base;
    protected GlideManager Gm;

    @Override
    protected View getPreviewLayout(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.layempty, container, false);
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState)
    {
        super.onCreateViewLazy(savedInstanceState);
        base = getActivity();
        // 创建Presenter
        mPresenter = createPresenter();
        // View与Presenter建立关联
        mPresenter.attachView((V) this);
        tools= GlobalTools.getInstance();
        Gm=new GlideManager(base);
        view = inflater.inflate(R.layout.flm_ui_base, null);    // 找到控件fragment的空地方
        fl_baseFragment_bottom = (FrameLayout) view.findViewById(R.id.fl_baseFragment_store);
        failnetworkd = (NetworkLayout) view.findViewById(R.id.failnetworkd);
        fl_baseFragment_bottom.addView(addChildView(fl_baseFragment_bottom,setLayoutResID()));

//        inflater.inflate(setLayoutResID(), fl_baseFragment_bottom, true);// 添加一个布局进来
        mTitleBarLayout = (TitleBarLayout) view.findViewById(R.id.tlb_flm_base_titlebar);
        //因为是fragment，基本不需要返回，所以隐藏返回，如果需要扩展，再到TitleBarLayout去设置布局逻辑
        mTitleBarLayout.setLefeShow(false,0,null);
        //沉浸占位的view
        stater = view.findViewById(R.id.view_base_flm_state);
        int statusBarHeight = GlobalTools.getInstance().getStatusBarHeight(getActivity());
        ViewGroup.LayoutParams layoutParams = stater.getLayoutParams();
        layoutParams.height = statusBarHeight;
        stater.setLayoutParams(layoutParams);

        setContentView(view);
        initData();
        initView();

    }

    private View addChildView(FrameLayout  fragmentview,int layoutResId) {
        mBaseView = LayoutInflater.from(base).inflate(layoutResId, fragmentview, false);
        mUnbinder = ButterKnife.bind(this, mBaseView);
        return mBaseView;
    }

    /**
     * 创建泛型的Presenter
     *
     * @return
     */
    protected abstract T createPresenter();


    /**
     * 构造方法描述:使findViewById不需要再强转
     */
    protected <T extends View> T BaseFindView(int resId)
    {
        return (T) (view.findViewById(resId));
    }

    /**
     * 隐藏标题栏
     **/
    protected void hideFlmTitleBarLayout()
    {
        mTitleBarLayout.setVisibility(View.GONE);
    }

    /**
     * 设置全屏沉浸模式显示，不要状态栏跟标题栏
     **/
    protected void setFullScreen()
    {
        hideFlmTitleBarLayout();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            // 透明状态
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }



    protected void showStateView(boolean isTag)
    {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            if (isTag)
            {
               getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
             //   getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                stater.setVisibility(View.VISIBLE);
            }
            else{ stater.setVisibility(View.GONE);}

        }

    }

    /**
     * 设置布局资源的视图控件ID抽象方法(当前Fragment绑定的布局),继承它的子类去实现
     **/
    protected abstract int setLayoutResID();

    /**
     * Fragment初始化视图的数据的抽象方法,继承它的子类去实现
     **/
    protected abstract void initData();

    /**
     * Fragment初始化视图的抽象方法,继承它的子类去实现
     **/
    protected abstract void initView();



    /**
     * 解除关联
     */
    @Override
    public void onDestroy()
    {
        super.onDestroy();

        if(mPresenter!=null){
            if (mPresenter.isViewAttached())
            {
                mPresenter.detachView();
            }
        }


        OkGo.getInstance().cancelTag(this);
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }

    }

    //获取缓存的信息
  /*  protected personxinxi getUserInfo()
    {
        LoginBean userInfo = (LoginBean) tools.readObject(base, Configs.SAVE_LOGIN_KEY, Configs.SAVE_LOGIN_SP_NAME);
        if (userInfo != null)
        {
            return userInfo.getData();
        } else
        {
            return null;
        }
    }

    protected LoginBean getUserInfo1()
    {
        LoginBean userInfo = (LoginBean) tools.readObject(base, Configs.SAVE_LOGIN_KEY, Configs.SAVE_LOGIN_SP_NAME);
        if (userInfo != null)
        {
            return userInfo;
        } else
        {
            return null;
        }
    }*/
    //监听手机键盘的弹起跟收回
    protected void onSoftKeyBoardListener(View view)
    {
        SoftKeyBoardListener.setListener(base, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener()
        {
            @Override
            public void keyBoardShow(int height)
            {

            }

            @Override
            public void keyBoardHide(int height)
            {

            }
        }, view);
    }

   /* protected  String getToken()
    {
        LoginBean userInfo = (LoginBean) tools.readObject(base, Configs.SAVE_LOGIN_KEY, Configs.SAVE_LOGIN_SP_NAME);
        if (userInfo != null)
        {
            return userInfo.getData().getTicket();
        } else
        {
            return "";
        }
    }*/

}
